<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Goods as ModelGoods;
use app\common\model\goods\Review;
use app\common\model\order\Point;
use app\common\model\ScoreLog;
use think\Db;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品分类
     *
     */
    public function category()
    {
        $list = Category::all(['type' => 'goods']);
        $this->success('', $list);
    }
    /**
     * 商品列表
     *
     */
    public function goodsList()
    {
        $get = request()->get();
        extract($get);
        if (!isset($cid) || empty($cid)) {
            $this->error('参数错误');
        }
        $goods = new ModelGoods;
        if (isset($keywords) && !empty($keywords)) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        $list = $goods->where('category_id', $cid)->paginate(10);
        $this->success('', $list);
    }
    /**
     * 商品详情
     *
     */
    public function goodsInfo()
    {
        $get = request()->get();
        extract($get);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $goods = new ModelGoods;
        $data = $goods->get($id);
        if ($data)
            $data['cart_num'] = ModelGoods::getCartNum($id, $this->auth->id);
        $this->success('', $data);
    }
    /**
     * 商品评论
     *
     */
    public function goodsReview()
    {
        $get = request()->get();
        extract($get);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $review = new Review();
        $data = $review->with('user')->where('goods_id', $id)->paginate((int)$limit ?? 10);
        $this->success('', $data);
    }
    /**
     * 积分商品列表
     *
     */
    public function integralGoodsList()
    {
        $goods = new ModelGoods;
        if (isset($keywords) && !empty($keywords)) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        $list = $goods->where('pay_switch', 1)->paginate(10);
        $this->success('', $list);
    }
    public function pointConversion()
    {


        $goods = new ModelGoods;
        if (isset($keywords) && !empty($keywords)) {
            $goods->whereLike('title', '%' . $keywords . '%');
        }
        $list = $goods->where('pay_switch', 1)->paginate(10);
        $this->success('', $list);
    }
    /**
     * 积分订单预览
     *
     */
    public function yuOrder()
    {
        $get = request()->get();
        $uid =  $this->auth->id;
        extract($get);
        $goods = ModelGoods::get($goods_id);
        $Point = new Point();
        $data = $Point->yuOrder($goods,$num, $address_id ?? 0, $uid);
        cache('point_' . $uid, $data, '900');
        $this->success('', $data);
    }
    /**
     * 积分兑换
     *
     */
    public function addOrder()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }

        $user = $this->auth->getUserinfo();
        $uid = $this->auth->id;
        $data = cache('point_' . $uid);

        if (!$data) {
            $this->error('订单已失效，请重新选择商品');
        }
        extract(is_object($data) ? $data->toArray() : $data);
        if ($user['score'] < $data['integral']) {
            $this->error('兑换积分不足');
        }
        if (!isset($address_id) || empty($address_id)) {
            $this->error('收货地址不能为空');
        }
        Db::startTrans();
        try {
            //添加订单商品信息
            $point = new Point();
            $order_id = $point->addData($data, $uid);
            if (!$order_id) {
                exception('下单失败');
            }
            ModelGoods::reduceInventory($data['id'], 1);
            $order = Point::get($order_id);
            ScoreLog::income($uid, $order->pay_point, 1, 300, $order->sn);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return self::error($e->getMessage() . $e->getLine());
        }
        cache('point_' . $uid, null);
        return $this->success('下单成功', $order_id);
    }
    /**
     * 积分订单确认收货
     *
     */
    public function confirmOrder()
    {
        if (!$this->request->isPost()) {
            $this->error('提交类型错误');
        }
        $post = $this->request->post();
        extract($post);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $point = new  Point;
        $order = $point->get($id);
        if (!$order) {
            $this->error('订单不存在');
        }
        $res = $order->save(['status' => 3, 'taketime' => time()]);
        if ($res) {
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }
}
